import { Reducer, Effect, Subscription} from "umi";
import {editRemote, getRemoteList, createTransaction, deleteTransaction, getPageInfoList} from './service';

interface ResponseState {
  flag: boolean,
  code: number,
  message: string,
  data: [];
}

interface TransactionDetailModelType {
  namespace: 'TransactionDetail';
  state: {
    data: {
      list: [],
      pageNum: number,
      pageSize: number
      //total: number,
    }
  };
  reducers: {
    getList: Reducer;
    getNewList: Reducer;
  },
  effects: {
    getRemote: Effect;
    getPageInfoRemote: Effect;
    edit: Effect;
    add: Effect;
    delete: Effect;
  },
  subscriptions: {
    setup: Subscription;
  }
}

const Model: TransactionDetailModelType = {
  //唯一标识
  namespace: 'TransactionDetail',
  state: {
    data: {
      list: [],
      pageNum: 1,
      pageSize: 20,
      //total: 3,
    }
  },
  reducers: {
    // （function 省略） function_name(state, action){return newState;}
    //格式为 (state, action) => newState 或 [(state, action) => newState, enhancer]
    //action对象可以分为：
          // {
          //   //函数名
          //    type: 'getList',
          //   //其他参数，一般放在payload中
          //    payload:[]
          // }
    // action可以变为[type, payload]，type如果没用可以去掉

    getList(state, action){
      console.log('reducer获得列表数据getList！');
      return action.payload;
      // const data = [
      //   {
      //     "transId": 1,
      //     "custId": 1,
      //     "account": "896522156",
      //     "cardNbr": "1234567894561234",
      //     "tranno": 2489,
      //     "monthNbr": 6,
      //     "bill": 65.2,
      //     "transType": "还款",
      //     "txnDatetime": "2020-07-04T08:51:52.000+00:00"
      //   },
      //   {
      //     "transId": 2,
      //     "custId": 1,
      //     "account": "1234567891",
      //     "cardNbr": "1234567894589545",
      //     "tranno": 1949,
      //     "monthNbr": 6,
      //     "bill": 52.3,
      //     "transType": "消费",
      //     "txnDatetime": "2020-06-04T08:11:27.000+00:00"
      //   },
      //   {
      //     "transId": 6,
      //     "custId": 3,
      //     "account": "2265689465",
      //     "cardNbr": "5654984984949849",
      //     "tranno": 1556,
      //     "monthNbr": 6,
      //     "bill": 654,
      //     "transType": "消费",
      //     "txnDatetime": "2020-06-09T09:09:53.000+00:00"
      //   },
      //   {
      //     "transId": 7,
      //     "custId": 3,
      //     "account": "2265689465",
      //     "cardNbr": "2498498495565498",
      //     "tranno": 6546,
      //     "monthNbr": 6,
      //     "bill": 989,
      //     "transType": "消费",
      //     "txnDatetime": "2020-06-10T09:11:53.000+00:00"
      //   },
      //   {
      //     "transId": 8,
      //     "custId": 3,
      //     "account": "2265689465",
      //     "cardNbr": "5465498454654498",
      //     "tranno": 6549,
      //     "monthNbr": 6,
      //     "bill": 854,
      //     "transType": "还款",
      //     "txnDatetime": "2020-06-10T01:12:30.000+00:00"
      //   }
      // ];
      // if(action.payload){
      //   console.log('reducer获得列表数据getList！', action.payload.data);
      //   return {data: action.payload.data};
      // }
      //  return {data};
    },

    getNewList(state, action){
      console.log('reducer获得列表数据getNewList！');
      return action.payload;
    }

  },
  effects: {
    //Generator函数 （function 省略） *function_name(action, effects){ }
    //格式为 *(action, effects) => void 或 [*(action, effects) => void, { type }]
    //action可以改为[type, payload]，effects里面有[put，call]...
    *getRemote(action, effects){
      console.log('getRemote获得后端列表数据！');
      const data = yield effects.call(getRemoteList);
      yield effects.put({
        type: 'getList',
        payload: {
          data
        },
      })
    },

    *getPageInfoRemote({payload: {pageNum, pageSize}}, effects){
      console.log('getPageInfoRemote获得分页列表数据！',pageNum, pageSize);
      const data = yield effects.call(getPageInfoList, pageNum, pageSize);
      console.log('getPageInfoRemote data！',data);
      yield effects.put({
        type: 'getList',
        payload: {
          data
        },
      })
    },

    *edit({payload}, {call, put, select}){
      console.log('编辑edit输入数据！:', payload.values);
      const data = yield call(editRemote, payload.values.transId, payload.values);
      const {pageNum, pageSize} = yield select((state) => {
        console.log('state仓库数据！:', state.TransactionDetail);
        return state.TransactionDetail.data;
      });
      yield put({
        type: 'getPageInfoRemote',
        payload: {
          pageNum,
          pageSize,
        },
      })
    },

    *add({payload}, {call, put, select}){
      console.log('新建add输入数据！:', payload.values);
      const data = yield call(createTransaction, payload.values);
      const {pageNum, pageSize} = yield select((state) => {
        console.log('state仓库数据！:', state.TransactionDetail);
        return state.TransactionDetail.data;
      });
      yield put({
        type: 'getPageInfoRemote',
        payload: {
          pageNum,
          pageSize,
        },
      })
    },

    //select 获得仓库数据
    *delete({payload}, {call, put, select}){
      console.log('删除delete！:', payload.record.transId);
      const data = yield call(deleteTransaction, payload.record.transId);
      const {pageNum, pageSize} = yield select((state) => {
        console.log('state仓库数据！:', state.TransactionDetail);
        return state.TransactionDetail.data;
      });
      yield put({
        type: 'getPageInfoRemote',
        payload: {
          pageNum,
          pageSize,
        },
      })
    },

  },
  subscriptions:{
    // 格式为 ({ dispatch, history }, done) => unlistenFunction
    // （function 省略,done 可省略） function_name({ dispatch, history}, done ){ }

    setup({ dispatch, history}){

      return history.listen((location, action )=>{
        if(location.pathname === '/transaction-detail'){
          console.log('subscription 订阅 getRemote:', location.pathname);
          dispatch({
            //走effects
            type: 'getPageInfoRemote',
            //直接走reducers
            // type: 'getList',
            payload: {
              pageNum: 1,
              pageSize: 10,
            }
          });
        }
      })
    }
  }
}
export default Model
